//bench.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({
  data: {
          inputName:'',
          owners: [],
          oindex: 0,
  },

  onLoad: function (options) {
        
          wx.setNavigationBarTitle({
                  title: '个人库存'
          })
          let curUid=options.uid?options.uid:'';
          let curUname = options.uname ? options.uname:'';
          let isStockAdmin = options.isStockAdmin && options.isStockAdmin>0?1:0;
          this.setData({
            curUid:curUid,
            curUname: curUname,
            isStockAdmin: isStockAdmin,
            scrollHeight: app.getScrollHeight(155)
          });
          this.getUsers();
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    // Do something when page show.
  },
  onHide: function () {
    // Do something when page hide.
  },
  onUnload: function () {
    // Do something when page close.
  },

        inputName: function (e) {
                let inputName = e.detail.value
                this.setData({
                        inputName: inputName
                })
        },
        searchGoods:function(){
                let that=this;
                that.getOwnerGoods();
        },
        toggleTab: function (e) {
                let id = e.currentTarget.dataset.id;
                if(id==this.data.tabNum){
                    id='';    
                }
                this.setData({ tabNum: id });
                this.getOwnerGoods();
        },
        getOwnerGoods: function () {
                let that = this
                let params = {};
                let tabNum = that.data.tabNum;
                let inputName = that.data.inputName;
                if (tabNum) {
                        params.categoryId = tabNum;
                }
                if (inputName) {
                        params.goodsName = inputName;
                        params.name = inputName;
                }
                
                params.uid = that.data.owners[that.data.oindex].masterUid;

                lbbService.getOwnerGoods(params, function (res) {
                        that.setData({
                                warehouseGoods: res.data
                        })
                })

                
        },
       
        getGoodsCategoryList: function () {
                let that = this;
                lbbService.getGoodsCategoryList({}, function (res) {
                        //let tabNum = res.data[0].id;
                        that.setData({
                                categories: res.data,
                                tabNum: '',
                        });
                        that.getOwnerGoods();
                })
        },

        getUsers: function () {
                let that = this
                if (!that.data.isStockAdmin){
                    let ownerNames=[];
                    let owners=[];
                    let a = { masterUid: that.data.curUid, masterName:that.data.curUname};
                    owners.push(a);
                    ownerNames.push(that.data.curUname);
                    that.setData({
                      ownerNames: ownerNames,
                      owners: owners
                    })
                    that.getGoodsCategoryList();
                }else{
                  lbbService.getStockUsers({ }, function (res) {
                    let userNames = [];
                    res.data.forEach(item => {
                      userNames.push(item.masterName)
                    })
                    that.setData({
                      ownerNames: userNames,
                      owners: res.data
                    })
                    that.getGoodsCategoryList();
                  })
                }
                
        },

        ownerChange: function (e) {
                this.setData({
                        oindex: e.detail.value
                })
                this.getOwnerGoods()
        },
        toRunningWater:function(e){
                let item=e.currentTarget.dataset.item;
                let params = '?tabNum=2&goodsStatId=' + item.goodsId;
                params += ('&goodsName='+item.goods.name)
                params += ('&oid=' + this.data.owners[this.data.oindex].masterUid)
                wx.navigateTo({
                        url: '../entryOutLog/entryOutLog'+params,
                })
        },
        
})